package com.sunwoda.demo.utils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Description:
 *
 * @author shiguorang
 * @date 2021-03-18 19:39:34
 */
public class JsonUtil {

    private static ObjectMapper mapper = new ObjectMapper();

    /**
     * @Author shiguorang
     * @Description //TODO
     * @Date 19:40:19 2021-03-18
     * @Param
     * @return
     **/
    public static String beanToJson(Object data){
        try {
            String result = mapper.writeValueAsString(data);
            return result;
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Author shiguorang
     * @Description //TODO
     * @Date 19:44:32 2021-03-18
     * @Param
     * @return
     **/
    public static <T> T jsonToBean(String jsonData,Class<T> beanType){
        try {
            T result = mapper.readValue(jsonData, beanType);
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
